package bbalgae.swing.outline;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.border.EmptyBorder;

/**
 * 팝업메뉴
 * 메뉴바에 종속되지 않고 항상 화면에 나타나지 않지만 특정 이벤트시 이벤트를 일으킨 위치에 표시하는 메뉴 컴포넌트
 * 주로 마우스에 의해 메뉴가 표시되기 때문에 MouseListener를 구현해야함
 * 하지만, 지금 당장 필요없는 메소드드들까지 재정의해야 하는 불편함이 발생! 따라서, MouseListener의인터페이스의
 * base class인 MouseAdapter를 상속받아필요한 메소드만정의하도록 함.
 * 
 * 
 *
 */

public class J10_HelloSwingPopupMenu extends JFrame {

	private JPanel contentPane;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					J10_HelloSwingPopupMenu frame = new J10_HelloSwingPopupMenu();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public J10_HelloSwingPopupMenu() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		
		//팝업메뉴 객체 생성
		JPopupMenu popupMenu = new JPopupMenu();
		//JFrame의 addPopup메소드를 이용해서 팝업메뉴 객체를 등록함.
		addPopup(contentPane, popupMenu);
		
		JMenu menu = new JMenu("보기");
		popupMenu.add(menu);
		
		JMenu mnNewMenuItem = new JMenu("New Menu Item");
		popupMenu.add(mnNewMenuItem);
		
		JMenu mnNewMenuItem_1 = new JMenu("New menu Item");
		popupMenu.add(mnNewMenuItem_1);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("New menu item");
		mnNewMenuItem_1.add(mntmNewMenuItem);
		
		JMenu mnNewMenu = new JMenu("New menu");
		mnNewMenuItem_1.add(mnNewMenu);
		
		JMenu mnNewMenuItem_2 = new JMenu("New menu Item");
		popupMenu.add(mnNewMenuItem_2);
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}

class MouseListenerAdapter extends MouseAdapter{ //마우스 리스너의 기본클래스
	
@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		super.mousePressed(e);
	}	
	@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			super.mouseReleased(e);
		}
}

class WindowListenerAdapter extends WindowAdapter{
	
	
}


class MouseActionListener implements MouseListener{
	@Override
	public void mouseClicked(MouseEvent arg0) {
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
	
			
		}

	@Override
	public void mouseExited(MouseEvent arg0) {
	
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
	
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {

		
	}
}
class WindowActionListener implements WindowListener{

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
}